<template>
	<view class="content">
		<!-- 滚动通知栏的方式 -->
		<uni-notification-center model="notice-bar"></uni-notification-center>
		<uni-badge size="small" :text="unreadCount" absolute="rightTop" type="primary">
			<navigator url="/uni_modules/uni-notification-center/pages/index/index" open-type="switchTab">
				<button>打开消息列表</button>
			</navigator>
		</uni-badge>
		
		<button @click="refreshUnreadCount">刷新未读消息数</button>
	</view>
</template>

<script>
	import uncUtils from '@/uni_modules/uni-notification-center/utils/main.js';
	export default {
		data() {
			return {
				unreadCount: 0
			}
		},
		mounted() {
			uncUtils.bindUnreadCount(unreadCount => {
				console.log('unreadCount-/*-**--*',unreadCount);
				this.unreadCount = unreadCount
				if (unreadCount) {
					uni.setTabBarBadge({
						index: 1,
						text: unreadCount.toString()
					})
				} else {
					uni.removeTabBarBadge({
						index: 1
					})
				}
			})
			
			// 把所有消息设置为未读---测试期间使用
			
			// const db = uniCloud.database();
			// db.collection('uni-notification-center').update({
			// 	is_read: false
			// })
		},
		methods: {
			refreshUnreadCount() {
				uncUtils.refreshUnreadCount()
			}
		}
	}
</script>

<style>
	.content {
		padding:0 20px;
	}
</style>
